添加if / else时,在形式参数之后的SyntaxError:missing)

时间:2017-09-11 09:23:05

标签: javascript jquery handsontable

任何人都可以告诉我我做错了什么,我想在handsontable中为不同的用户提供不同的观点。我将表定义为

document.addEventListener("DOMContentLoaded", function() {
  var create_table = document.getElementById('create_table');
  var mydata = document.getElementById('mydata').innerHTML;//to get the hidden element
  var logged_user = document.getElementById('logged_user').innerHTML;// to get remote user
  var plan_creator = document.getElementById('plan_creator').innerHTML;//to get the person who has created the plan
  console.log(logged_user  + "  " + plan_creator);
  console.log(mydata);
  var searchResultCount=0;
  var hot,searchFiled,resultCount;
function searchResultCounter(instance, row, col, value, result) {
    Handsontable.plugins.Search.DEFAULT_CALLBACK.apply(this, arguments);

    if (result) {
      searchResultCount++;
    }
}
var buttons = {
    save:document.getElementById('save'),
    load:document.getElementById('load'),
    file:document.getElementById('file_export')
    }
    var objectData = JSON.parse(mydata);//to decode data in JSON format
    console.log(objectData);
    hot = new Handsontable(create_table, {
    data:objectData,
    colHeaders: true,
    rowHeaders: true,
    contextMenu: true,
    minRows: 30,
    minCols: 13,
    maxCols:18,
    maxRows:100,
    copyPaste:true,
    dropdownMenu: true,//plugin to display menu on column header
    filters:true,
    columnSorting:true,//plugin to enable sorting
    sortIndicator:true,//to display sorting is done
    comments:true,//to add comments
    colHeaders:["Testcase Name","Cell Name","Customer","Flops","Title","Status","Mfix CCR","Scenerio Brief Description","Expected Results","CCR Status","CCR No","Remarks","Testcase Path"],
if(logged_user == plan_creator) {
    columns:[//when using this not able to remove column
              {data:'tc_name'},
              {data:'cell_name'},
              {data:'customer_name'},
              {data:'flops' ,type:'numeric'},
              {data:'title'},
              {data:'status'},
              {data:'mfix_ccr'},
              {data:'test_scenerio'},
              {data:'expected_results'},
              {data:'ccr_status'},
              {data:'ccr_num'},
              {data:'remarks'},
              {data:'tc_path'}],
    }
    else{
    columns:[//when using this not able to remove column
              {data:'tc_name' ,readOnly:true } ,
              {data:'cell_name',readOnly:true },
              {data:'customer_name',readOnly:true },
              {data:'flops' ,type:'numeric',readOnly:true },
              {data:'title',readOnly:true },
              {data:'status',readOnly:true },
              {data:'mfix_ccr',readOnly:true },
              {data:'test_scenerio',readOnly:true },
              {data:'expected_results',readOnly:true },
              {data:'ccr_status',readOnly:true },
              {data:'ccr_num',readOnly:true },
              {data:'remarks'},//only remarks can be added by other user
              {data:'tc_path',readOnly:true }],
    }
    search: {
     callbak:searchResultCounter
    }
});
   searchFiled = document.getElementById('search_filed');
   resultCount=document.getElementById('resultCount');
   var exportPlugin=hot.getPlugin('exportFile');
   Handsontable.dom.addEvent(searchFiled, 'keyup', function(event) {
    var queryResult;
    console.log(this.value);
    searchResultCount = 0;
    queryResult = hot.search.query(this.value);
    console.log(queryResult);
    //resultCount.innerText = searchResultCount.toString();
    hot.render();
});
buttons.file.addEventListener('click', function() {// enabling the plugin to download the file
    exportPlugin.downloadFile('csv', {filename: 'MyFile',columnHeaders:true});
  });

当我删除if / else语句并且只使用一个scenerio时,我没有收到任何错误。当使用上面的代码我得到错误,但是当我删除if / else部分并且只使用columns属性时以一种简单的方式,我没有得到这个错误。但我想对计划的创建者和其他人有不同的看法。

还有其他办法吗?

由于

2 个答案:

答案 0 :(得分:1)

构建对象时不能使用if语句,但可以使用三元 ?: 运算符,如下所示:

colHeaders: ... ,
columns: logged_user == plan_creator
    ? /* value in case they are equal */
    : /* value in case they are not equal */,
search: ...

答案 1 :(得分:0)

而不是使用if else

if(logged_user == plan_creator) {
columns:[//when using this not able to remove column
  {data:'tc_name'},
  {data:'cell_name'},
  {data:'customer_name'},
  {data:'flops' ,type:'numeric'},
  {data:'title'},
  {data:'status'},
  {data:'mfix_ccr'},
  {data:'test_scenerio'},
  {data:'expected_results'},
  {data:'ccr_status'},
  {data:'ccr_num'},
  {data:'remarks'},
  {data:'tc_path'}],
}

其他{}

你可以使用三元运算符:

columns: (logged_user === plan_creator)
  ? [//when using this not able to remove column
      {data:'tc_name'},
      ...
    ] 
  : [//when using this not able to remove column
      {data:'tc_name' ,readOnly:true },
      ... 
    ]