سؤال

I am using w2ui(http://w2ui.com/) plugin.

Consider this example: 1

  1. I want when the add button is clicked an empty editable row be appeared in the grid to add a new row?

  2. I did not find a config to hide search box, how can I hide it?

  3. How can I set a column as id? currently it just recognize recid.
هل كانت مفيدة؟

المحلول

Here is a sample how to add new records in the grid when a button is clicked:

    <html>
    <head>
    <link rel="stylesheet" type="text/css" media="screen" href="../css/w2ui.css" /> 
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/w2ui.js"></script>
    <script>
    $(function () {
      $('#grid').w2grid({ 
        name: 'grid', 
        show: { 
          toolbar: true,
          footer: true,
          header: true,
          columnHeaders: true,
          lineNumbers: true,
          toolbarDelete: true,
          toolbarSave: true,
          toolbarAdd: true
        },
        columns: [        
          { field: 'recid', caption: 'ID', size: '50px', sortable: true, resizable: true, searchable: 'int' },
          { field: 'lname', caption: 'Last Name', size: '30%', sortable: true, resizable: true, searchable: true,
            editable: { type: 'text' }
          },
          { field: 'fname', caption: 'First Name', size: '30%', sortable: true, resizable: true, searchable: true,
            editable: { type: 'text' }
          },
        ],
        onAdd: function (target, data) {
          var recid = 1;
          if (this.records.length > 0) recid = (Math.max.apply(Math, this.find({}))) + 1;
          this.add({ recid: recid });
          $('#grid_grid_edit_'+ (this.records.length - 1) +'_1').focus();
        },
        onSave: function (target, data) {
          var obj = this;
          console.log(data);
          data.onComplete = function () {
            for (var r in data.changed) {
              obj.get(data.changed[r].recid).editable = false;
            }
            obj.refresh();
          }
        }
      });
    });
    </script>
    </head>
    <body>
      <div id="grid" style="width: 100%; height: 500px;"></div>
    </body>
    </html>

نصائح أخرى

  1. Since it is a bigger question. I will answer it in the next post.

  2. To hide search box, you can do this by setting w2grid.show.toolbarSearch = false; See http://w2ui.com/web/docs/w2grid.show for more info.

  3. recid is a required unique field. It has to be. However, you might have any number of additional columns which do not have to be visible or even have corresponding elements in columns array. If server side returns id as a unique column, you might add event listener onLoad and loop thru the records setting recid as id. I know is it not as pretty, but it will get the job done. Changing the server side to return recid will make it pretty.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top